﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartSpace.OA
{
    public class OADbContext : DbContext
    {
        public OADbContext()
            : base("name=" + SmartSpace.Configuration.ConnectionStringName["oa"])
        {
            if (SmartSpace.Configuration.IsAutomaticMigrations)
            {
                Database.SetInitializer<OADbContext>(new MigrateDatabaseToLatestVersion<OADbContext, DBMC>());
            }
            else
            {
                Database.SetInitializer<OADbContext>(null);
            }
        }

        public DbSet<LeaveType> LeaveTypeSet { get { return Set<LeaveType>(); } }

        public DbSet<DocFile> DocFileSet { get; set; }

        public DbSet<Asset> AssetSet { get; set; }

        public DbSet<Article> ArticleSet { get; set; }
        public DbSet<AttendanceRecord> AttendanceRecordSet { get; set; }

        public DbSet<WorkLog> WorkLogSet { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LeaveRequest>().ToTable("oa_leaverequest");
            modelBuilder.Entity<LeaveRequest>().HasKey(p => p.Id);

            modelBuilder.Entity<DocItem>().ToTable("oa_docitem");
            modelBuilder.Entity<DocItem>().HasKey(p => p.Id);

            base.OnModelCreating(modelBuilder);
        }
    }

    internal sealed class DBMC : DbMigrationsConfiguration<OADbContext>
    {
        public DBMC()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
    }
}
